<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      class="search"
    >
      <el-form-item label="方案名称">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入方案名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" border height="300px">
      <el-table-column
        label="方案名称"
        min-width="120"
        align="center"
        show-overflow-tooltip
        prop="planName"
      />
      <el-table-column
        label="一级佣金/元"
        min-width="140"
        align="center"
        show-overflow-tooltip
        prop="primaryCommissionAmount"
      />
      <el-table-column
        label="一级分佣金百分比(%)"
        min-width="160"
        align="center"
        show-overflow-tooltip
        prop="primaryCommissionPercentage"
      />
      <el-table-column
        label="二级佣金/元"
        min-width="120"
        align="center"
        show-overflow-tooltip
        prop="secondaryCommissionAmount"
      />
      <el-table-column
        label="二级分佣金百分比(%)"
        min-width="160"
        align="center"
        show-overflow-tooltip
        prop="secondaryCommissionPercentage"
      />
      <el-table-column
        label="操作"
        width="140"
        align="center"
        class-name="small-padding fixed-width"
        fixed="left"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handDetail(scope.row)"
            >选择该方案</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-count="5"
    />
  </div>
</template>

<script>
import { exportMkOrder } from '@/api/marketing/mkOrder'
import * as CommissionApi from '@/api/marketing/commission.js'
import * as QmyxApi from '@/api/marketing/qmyx.js'

export default {
  name: 'MkOrder',
  dicts: ['commission_type', 'businss_mode'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 全名营销订单表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: '', // planName
        planName: '', // 方案名称
      },
      showxinzheng: true,
      // 表单参数
      form: {
        associatedBusinessNo: '', // 关联业务编号（数据字典维护）
        associatedBusinessName: '', // 关联业务名称（数据字典维护）
        connectionWayNo: '', // 关联方式编号
        connectionWayName: '', // 关联方式名称
        commissionId: 'xxxxxx', // 佣金方案id
        associationModeDto: [
          // {
          //     associationModeId: "xxxxxx", // 关联方案/车型/品类/活动id
          //     associationModeName: "xxxxxx", // 关联方案/车型/品类/活动名称
          // },
        ], // 关联业务
      },
      // 表单校验
      rules: {
        planName: [
          { required: true, message: '请输入方案名称', trigger: 'blur' },
        ],
        primaryAmountChecked: [
          { required: true, message: '请输入一级分销佣金', trigger: 'blur' },
        ],
        secondaryAmountChecked: [
          { required: true, message: '请输入二级分销佣金', trigger: 'blur' },
        ],
      },
      isEdit: false,
      showxiangiqng: false,
      xiangqing: {},
      checkboxGroup1: [],
      connectionWayList: [],
      bessusinList: [], //租售业务类型
      planList: [], // 租售方案
      modelList: [], // 租售车型
      getZsSystemActivityData: [], // 营销系统获取活动
      sanjilist: [], // 三级列表
      sanjimap: {}, // 三级映射
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询全名营销订单列表 */
    getList() {
      this.loading = true
      QmyxApi.plan(this.queryParams).then((response) => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        createTime: '', // 创建时间
        planName: '', // 方案名称
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.handleQuery()
    },
    /** 详情按钮操作 */
    handDetail(row) {
      this.$emit('select', row)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有全名营销订单数据项？')
        .then(() => {
          this.exportLoading = true
          return exportMkOrder(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
    /**
     * 创建方案
     */
    addfangan() {
      this.showxinzheng = true
    },
    /**
     * 提交创建方案
     */
    addsave() {
      if (this.isEdit) {
        CommissionApi.put(this.form)
          .then((res) => {
            console.log(res)
            this.$message.success(res.msg)
            this.showxinzheng = false
            this.handleQuery()
          })
          .catch((err) => {
            this.$message.error('添加失败')
          })
        return
      }
      CommissionApi.add(this.form)
        .then((res) => {
          console.log(res)
          this.$message.success(res.msg)
          this.showxinzheng = false
          this.handleQuery()
        })
        .catch((err) => {
          this.$message.error('添加失败')
        })
    },
    resetFrom() {
      this.form = {
        planName: '', // 方案名称
        primaryAmountChecked: 1, // 一级分销佣金:1按固定金额、2按百分比
        primaryCommissionAmount: 0, // 一级分销佣金金额
        primaryCommissionPercentage: 0, // 一级分销佣金百分比
        secondaryAmountChecked: 1, // 二级分销佣金:1按固定金额、2按百分比
        secondaryCommissionAmount: 0, // 二级分销佣金金额
        secondaryCommissionPercentage: 0, // 	二级分销佣金百分比
      }
      this.isEdit = false
    },
    yewuchange(data) {
      const item = this.dict.type.commission_type.find(
        (val) => val.value == data
      )
      this.form.associatedBusinessName = item.label
      QmyxApi.connectionWayList(item.raw.dictCode).then((res) => {
        this.connectionWayList = res.data
      })
    },
    fangshichange(item) {
      this.form.select = []
      this.form.connectionWayName = item.connectionWayName || item.planName
      if (this.form.associatedBusinessNo == 'ZSXT') {
        if (this.form.connectionWayNo == 1) {
          this.getplanList(item.connectionWayNo)
        }
        if (this.form.connectionWayNo == 2) {
          this.getmodelList(item.connectionWayNo)
        }
        if (this.form.connectionWayNo == 3) {
          this.getbessusinList(item.connectionWayNo)
        }
      }
      if (this.form.associatedBusinessNo == 'YXXT') {
        if (this.form.connectionWayNo == 1) {
          this.getgetZsSystemActivityData()
        }
      }
    },

    getbessusinList(associatedBusinessNo) {
      QmyxApi.bessusinList({
        pageNum: 1,
        pageSize: 999,
        associatedBusinessNo,
      }).then((res) => {
        this.bessusinList = res.rows
        this.setsanjilist(res.rows)
      })
    },

    getplanList(associatedBusinessNo) {
      QmyxApi.planList({
        pageNum: 1,
        pageSize: 999,
        associatedBusinessNo,
      }).then((res) => {
        this.planList = res.rows
        this.setsanjilist(res.rows)
      })
    },

    getmodelList(associatedBusinessNo) {
      QmyxApi.modelList({
        pageNum: 1,
        pageSize: 999,
        associatedBusinessNo,
      }).then((res) => {
        this.modelList = res.rows
        this.setsanjilist(res.rows)
      })
    },

    getgetZsSystemActivityData() {
      QmyxApi.getZsSystemActivityData({ pageNum: 1, pageSize: 999 }).then(
        (res) => {
          this.getZsSystemActivityData = res.rows
          this.setsanjilist(res.rows)
        }
      )
    },

    setsanjilist(list) {
      this.sanjilist = list.map((val) => {
        const associationModeId =
          val.associationModeId ||
          val.id ||
          val.seckillId ||
          val.connectionWayNo
        const associationModeName =
          val.associationModeName ||
          val.value ||
          val.seckillName ||
          val.connectionWayName
        const returnbody = {
          associationModeId,
          associationModeName,
        }
        this.sanjimap[associationModeId] = returnbody
        return returnbody
      })
    },

    sanjichange() {
      this.form.associationModeDto = this.form.select.map((val) => {
        return this.sanjimap[val]
      })
    },
  },
}
</script>
